package com.yanqu.road.dao.impl.activity.gourdchild;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.gourdchild.config.GourdLevelBookInfo;
import com.yanqu.road.entity.activity.gourdchild.config.GourdLevelInfo;
import com.yanqu.road.entity.activity.gourdchild.config.GourdLevelLotusInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GourdChildConfigDaoImpl extends TempDao {


    private static String getGourdLevelInfoSql = "select * from `t_s_gourd_level` where `activityId`=? ORDER BY `id` ASC";
    public List<GourdLevelInfo> getGourdLevelInfoList(int activityId) {
        List<GourdLevelInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGourdLevelInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GourdLevelInfo gourdLevelInfo = new GourdLevelInfo();
                gourdLevelInfo.setActivityId(rs.getInt("activityId"));
                gourdLevelInfo.setId(rs.getInt("id"));
                gourdLevelInfo.setType(rs.getInt("type"));
                gourdLevelInfo.setChapter(rs.getInt("chapter"));
                gourdLevelInfo.setLevel(rs.getInt("level"));
                gourdLevelInfo.setMonster(rs.getInt("monster"));
                gourdLevelInfo.setStageType(StringUtils.stringToIntegerList(rs.getString("stageType"), ";"));
                gourdLevelInfo.setMaxRound(rs.getInt("maxRound"));
                gourdLevelInfo.setAdviceDamage(rs.getInt("adviceDamage"));
                gourdLevelInfo.setEnergy(rs.getInt("energy"));
                gourdLevelInfo.setRandomReward(rs.getString("randomReward"));
                gourdLevelInfo.setReward(rs.getString("reward"));
                gourdLevelInfo.setDefeatReward(rs.getString("defeatReward"));
                gourdLevelInfo.setHp(rs.getInt("hp"));
                gourdLevelInfo.setStarLimit(StringUtils.stringToIntegerList(rs.getString("starLimit"), ";"));
                gourdLevelInfo.setElement(StringUtils.stringToIntegerList(rs.getString("element"), ";"));
                list.add(gourdLevelInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGourdLevelLotusInfoSql = "select * from `t_s_gourd_level_lotus` where `activityId`=? ORDER BY `level` ASC";
    public Map<Integer, GourdLevelLotusInfo> getGourdLevelLotusInfoList(int activityId) {
        Map<Integer, GourdLevelLotusInfo> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGourdLevelLotusInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GourdLevelLotusInfo info = new GourdLevelLotusInfo();
                info.setActivityId(rs.getInt("activityId"));
                info.setLevel(rs.getInt("level"));
                info.setDamage(rs.getInt("damage"));
                info.setConsume(rs.getString("consume"));
                result.put(info.getLevel(), info);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


    private static String getGourdLevelBookInfoSql = "select * from `t_s_gourd_level_book` where `activityId`=? ORDER BY `bookId` ASC";
    public Map<Integer, GourdLevelBookInfo> getGourdLevelBookInfoList(int activityId) {
        Map<Integer, GourdLevelBookInfo> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGourdLevelBookInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GourdLevelBookInfo info = new GourdLevelBookInfo();
                info.setActivityId(rs.getInt("activityId"));
                info.setBookId(rs.getInt("bookId"));
                info.setShowProbability(rs.getInt("showProbability"));
                info.setShowReward(rs.getInt("showReward"));
                info.setIsHide(rs.getInt("isHide"));
                info.setSort(rs.getInt("sort"));
                result.put(info.getBookId(), info);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }
}
